package hanjia;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Description:
 * Author:    Dane
 * CreateTime:2025/2/10-下午4:13
 * Since  :   1.0
 * Version:   1.0
 */
public class LeetCode15 {

    public static List<List<Integer>> threeSum(int[] nums) {

        List<List<Integer>> results = new ArrayList<>();
        Arrays.sort(nums);

        for (int i = 0; i < nums.length; i++) {
            if (i > 0 && nums[i] == nums[i - 1]) {
                continue;
            }
            int left = i + 1;
            int right = nums.length - 1;
            while (left < right) {
                if (nums[left] + nums[right] + nums[i] > 0) {
                    right--;
                } else if (nums[left] + nums[right] + nums[i] < 0) {
                    left++;
                } else {
                    ArrayList<Integer> result = new ArrayList<>();
                    result.add(nums[i]);
                    result.add(nums[left]);
                    result.add(nums[right]);

                    results.add(result);

                    while (left<right&&nums[right] == nums[right - 1]) {
                        right--;
                    }


                    while (left<right&&nums[left] == nums[left + 1]) {
                        left++;
                    }
                    left++;
                    right--;


                }


            }


        }
        return results;


    }

    public static void main(String[] args) {
        int nums[] = {-1, 0, 1, 2, -1, -4};
        threeSum(nums);
    }
}
